package com.salton123.compress

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.salton123.soulove.app.R
import com.salton123.soulove.common.Constants
import com.salton123.ui.base.BaseActivity
import com.salton123.utils.FFmpegCompressor
import java.io.File
import kotlinx.android.synthetic.main.activity_video_compress.tvInfo
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * Time:2022/1/20 6:10 下午
 * Author:
 * Description:
 */
@Route(path = Constants.Router.Video.COMPRESS)
class VideoCompressionActivity : BaseActivity() {
    override fun getLayoutId(): Int = R.layout.activity_video_compress

    override fun initViewAndData(savedInstanceState: Bundle?) {
        File(intent?.getStringExtra("filePath") ?: "").apply {
            if (exists()) {
//                MainScope().launch {
//                    val stringBuilder = StringBuilder()
//                    val help = FFmpegKit.execute("-h")
//                    stringBuilder.append(help.toString() + "\n")
//
//                    val encoders = FFmpegKit.execute("-encoders")
//                    stringBuilder.append(encoders.toString() + "\n")
//
//                    val filePath = absolutePath
//                    val fileName = name.replace(".mp4", "-emp.mp4")
//                    val newFilePath = parent + File.separator + fileName
////                    val command = "-i $filePath -c:v libx264 -hide_banner -threads 8 $newFilePath"
//                    //测试多次发现这个命令压缩率可以，感觉不到失真、
//                    val command = "-vcodec h264  -i $filePath -vcodec h264  -threads 8 $newFilePath"
//                    stringBuilder.append(command + "\n")
//                    val mediaInfo = FFprobeKit.getMediaInformation(filePath)
//                    FileIOUtils.writeFileFromString("/sdcard/mediaInfo.txt", mediaInfo.toString())
//                    stringBuilder.append(mediaInfo.toString() + "\n")
//                    val session = FFmpegKit.execute(command)
//                    FileIOUtils.writeFileFromString("/sdcard/session.txt", session.toString())
//                    stringBuilder.append(session.toString() + "\n")
//
//                    withContext(Dispatchers.Main) {
//                        tvInfo.text = stringBuilder.toString()
//                    }
//                }
                MainScope().launch {
                    FFmpegCompressor.compress(absolutePath) {
                        lifecycleScope.launch {
                            tvInfo.text = "进度：$it"
                        }
                    }
                }
            }
        }
    }
}